package com.summer.oauth.center.controller;

import com.summer.oauth.center.service.PublicService;
import com.summer.user.center.api.UserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

@RestController
@RequestMapping("/public")
public class PublicController {

    @Autowired
    private UserService userService;

    @Autowired
    private PublicService publicService;

    @GetMapping("/addTestTable")
    @GlobalTransactional
    public String addTestTable(){

        userService.test();

        int i=1/0;

        return "addTestTable";
    }


    @GetMapping("/lock")
    public String lock() throws InterruptedException {

//        publicService.print(2);
        CountDownLatch countDownLatch=new CountDownLatch(3);
        for (int i = 0; i < 3; i++) {
//            Thread.sleep(200);
            new Thread(()->{
                publicService.print();
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        return "lock";
    }


}
